package megrez.keycloak.providers.event;

import org.keycloak.events.Event;
import org.keycloak.events.admin.AdminEvent;

import lombok.experimental.UtilityClass;
import megrez.keycloak.common.Consts;
import megrez.keycloak.common.KeycloakAdminEvent;
import megrez.keycloak.common.KeycloakAuthDetails;
import megrez.keycloak.common.KeycloakEvent;
import megrez.keycloak.providers.mqtt.MqttUtils;

/**
 * 事件工具
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@UtilityClass
public class EventUtils {
    public KeycloakEvent of(Event event) {
        KeycloakEvent rslt = new KeycloakEvent();
        rslt.setId(event.getId());
        rslt.setTime(event.getTime());
        rslt.setType(event.getType() == null ? null : event.getType().name());
        rslt.setRealmId(event.getRealmId());
        rslt.setClientId(event.getClientId());
        rslt.setUserId(event.getUserId());
        rslt.setSessionId(event.getSessionId());
        rslt.setIpAddress(event.getIpAddress());
        rslt.setError(event.getError());
        rslt.setDetails(event.getDetails());
        return rslt;
    }

    public KeycloakAdminEvent of(AdminEvent event) {
        KeycloakAuthDetails details = null;
        if (event.getAuthDetails() != null) {
            details = new KeycloakAuthDetails();
            details.setRealmId(event.getAuthDetails().getRealmId());
            details.setClientId(event.getAuthDetails().getClientId());
            details.setUserId(event.getAuthDetails().getUserId());
            details.setIpAddress(event.getAuthDetails().getIpAddress());
        }

        KeycloakAdminEvent rslt = new KeycloakAdminEvent();
        rslt.setId(event.getId());
        rslt.setTime(event.getTime());
        rslt.setRealmId(event.getRealmId());
        rslt.setAuthDetails(details);
        rslt.setResourceType(event.getResourceTypeAsString());
        rslt.setOperationType(event.getOperationType() == null ? null : event.getOperationType().name());
        rslt.setResourcePath(event.getResourcePath());
        rslt.setRepresentation(event.getRepresentation());
        rslt.setError(event.getError());
        return rslt;
    }

    public String getTopic(final AdminEvent event) {
        String topic = Consts.MQTT_TOPIC_PREFIX_EVENT
                + "/ADMIN"
                + "/" + event.getRealmId()
                + "/" + (event.getError() != null ? "ERROR" : "SUCCESS")
                + "/" + event.getResourceTypeAsString()
                + "/" + event.getOperationType().toString();
        return MqttUtils.normalizeTopic(topic).toUpperCase();
    }

    public String getTopic(final Event event) {
        String topic = Consts.MQTT_TOPIC_PREFIX_EVENT
                + "/CLIENT"
                + "/" + event.getRealmId()
                + "/" + (event.getError() != null ? "ERROR" : "SUCCESS")
                + "/" + event.getClientId()
                + "/" + event.getType();
        return MqttUtils.normalizeTopic(topic).toUpperCase();
    }
}
